﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Lee.Utility.Security;

public class AesNewHelper
{

    private static string EnctyptKey = "123456789123456789123456789";
    private static string EnctyptKeyAdmin = "123456789123456789123456789";
    /// <summary>
    /// 加密初始化向量
    /// </summary>
    private static string EnctyptIV
    {
        get
        {
            return EnctyptKey.Substring(0, 16);
        }
    }

    private static string EnctyptIVAmin
    {
        get
        {
            return EnctyptKeyAdmin.Substring(0, 16);
        }
    }

    /// <summary>
    /// 测试
    /// </summary>
    public static void Test()
    {
        //加密初始化向量
        string message = "abcdefghigklmnopqrstuvwxyz0123456789";
        Console.WriteLine("加密前：" + message);
        message = Encrypt(message, EnctyptKey, EnctyptIV);
        Console.WriteLine("加密后：" + message);
        message = Decrypt(message, EnctyptKey, EnctyptIV);
        Console.WriteLine("解密后：" + message);
    }

    /// <summary>
    /// AES加密
    /// </summary>
    /// <param name="text">明文</param>
    /// <returns></returns>
    public static string Encrypt(string text)
    {
        return Encrypt(text, EnctyptKey, EnctyptIV);
    }

    /// <summary>
    /// AES加密
    /// </summary>
    /// <param name="text">明文</param>
    /// <param name="key">密钥</param>
    /// <param name="iv">向量</param>
    /// <returns></returns>
    public static string Encrypt(string text, string key, string iv)
    {
        RijndaelManaged rijndaelManaged = new RijndaelManaged();
        rijndaelManaged.Mode = CipherMode.CBC;
        rijndaelManaged.Padding = PaddingMode.PKCS7;
        rijndaelManaged.KeySize = 128;
        rijndaelManaged.BlockSize = 128;

        byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(key);

        #region 无用代码

        //byte[] keyBytes = new byte[16];
        //int len = pwdBytes.Length;
        //if (len > keyBytes.Length)
        //{
        //    len = keyBytes.Length;
        //}
        //System.Array.Copy(pwdBytes, keyBytes, len);
        //rijndaelManaged.Key = keyBytes;

        #endregion 无用代码

        rijndaelManaged.Key = pwdBytes;

        byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv);
        rijndaelManaged.IV = ivBytes;

        ICryptoTransform iCryptoTransform = rijndaelManaged.CreateEncryptor();
        byte[] textBytes = Encoding.UTF8.GetBytes(text);
        byte[] targetBytes = iCryptoTransform.TransformFinalBlock(textBytes, 0, textBytes.Length);
        return Convert.ToBase64String(targetBytes);
    }

    /// <summary>
    /// AES加密
    /// </summary>
    /// <param name="text">明文</param>
    /// <param name="openEncrypt">是否开启加密</param>
    /// <returns></returns>
    public static string TryEncrypt(string text, bool openEncrypt = true)
    {
        if (string.IsNullOrEmpty(text))
            return text;
        if (openEncrypt)
        {
            return Encrypt(text, EnctyptKey, EnctyptIV);
        }
        return text;
    }

    public static string TryEncryptAdmin(string text, bool openEncrypt = true)
    {
        if (string.IsNullOrEmpty(text))
            return text;
        if (openEncrypt)
        {
            return Encrypt(text, EnctyptKeyAdmin, EnctyptIVAmin);
        }
        return text;
    }

    /// <summary>
    /// AES解密
    /// </summary>
    /// <param name="text">密文</param>
    /// <param name="openEncrypt">是否开启加密/解密</param>
    /// <returns></returns>
    public static string TryDecrypt(string text, bool openEncrypt = true)
    {
        if (string.IsNullOrEmpty(text))
            return text;
        try
        {
            if (openEncrypt)
            {
                return Decrypt(text, EnctyptKey, EnctyptIV);
            }
        }
        catch (Exception ex) { }
        return text;
    }

    public static string TryDecryptAdmin(string text, bool openEncrypt = true)
    {
        if (string.IsNullOrEmpty(text))
            return text;
        try
        {
            if (openEncrypt)
            {
                return Decrypt(text, EnctyptKeyAdmin, EnctyptIVAmin);
            }
        }
        catch (Exception ex) { }
        return text;
    }

    /// <summary>
    /// AES解密
    /// </summary>
    /// <param name="text">密文</param>
    /// <returns></returns>
    public static string Decrypt(string text)
    {
        return Decrypt(text, EnctyptKey, EnctyptIV);
    }

    /// <summary>
    /// AES解密
    /// </summary>
    /// <param name="text">密文</param>
    /// <param name="key">密钥</param>
    /// <param name="iv">向量</param>
    /// <returns></returns>
    public static string Decrypt(string text, string key, string iv)
    {
        if (string.IsNullOrEmpty(text))
            return text;
        RijndaelManaged rijndaelManaged = new RijndaelManaged();
        rijndaelManaged.Mode = CipherMode.CBC;
        rijndaelManaged.Padding = PaddingMode.PKCS7;
        rijndaelManaged.KeySize = 128;
        rijndaelManaged.BlockSize = 128;

        byte[] encryptedData = Convert.FromBase64String(text);
        byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(key);

        #region 无用代码

        //byte[] keyBytes = new byte[16];
        //int len = pwdBytes.Length;
        //if (len > keyBytes.Length)
        //{
        //    len = keyBytes.Length;
        //}
        //System.Array.Copy(pwdBytes, keyBytes, len);
        //rijndaelManaged.Key = keyBytes;

        #endregion 无用代码

        rijndaelManaged.Key = pwdBytes;

        byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv);
        rijndaelManaged.IV = ivBytes;

        ICryptoTransform iCryptoTransform = rijndaelManaged.CreateDecryptor();
        byte[] targetBytes = iCryptoTransform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
        return Encoding.UTF8.GetString(targetBytes);
    }
}
